package com.jeff.tianti.cms.dao;

import com.jeff.tianti.common.dao.CustomBaseSqlDaoImpl;
import com.jeff.tianti.common.entity.PageModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jeff.tianti.cms.entity.ActivityAlerts;
import com.jeff.tianti.cms.dto.ActivityAlertsQueryDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author xujianfang
 * @desc ActivityAlertsDaoImpl��
 * @date 2019-07-11
 */
@Service
public class ActivityAlertsDaoImpl extends CustomBaseSqlDaoImpl implements ActivityAlertsDaoCustom {

    public PageModel<ActivityAlerts> queryActivityAlertsPage(ActivityAlertsQueryDTO activityAlertsQueryDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select t from ActivityAlerts t where 1 =1 ");
        if (activityAlertsQueryDTO != null) {
            String type = activityAlertsQueryDTO.getType();
            if (StringUtils.isNotBlank(type) && !"-1".equals(type)) {//0->协会快讯, 1->年度计划,2->重要活动
                hql.append(" and t.type = :type ");
                map.put("type", type);
            }
            String deleteFlag = activityAlertsQueryDTO.getDeleteFlag();
            if (StringUtils.isNotBlank(deleteFlag) && !"-1".equals(deleteFlag)) {
                hql.append(" and t.deleteFlag = :deleteFlag ");
                map.put("deleteFlag", deleteFlag);
            }
            String state = activityAlertsQueryDTO.getState();
            if (StringUtils.isNotBlank(state)) {
                hql.append(" and t.state = :state ");
                map.put("state", state);
            }
        }
        hql.append(" order by t.sort desc,t.createDate desc ");
        return this.queryForPageWithParams(hql.toString(), map, activityAlertsQueryDTO.getCurrentPage(), activityAlertsQueryDTO.getPageSize());
    }

    public List<Map<String, Object>> queryActivityAlertsList(ActivityAlertsQueryDTO activityAlertsQueryDTO) {
        StringBuilder hql = new StringBuilder();
        /*
        *   StringBuilder hql = new StringBuilder();
        hql.append(" SELECT id,DATE_FORMAT(create_date,'%Y-%m-%d') as createDate ,title ,publisher,source_from as sourceFrom ,content,root_column_info_id as rootColumnInfoId , guid_read as guidRead ,cover_image_url as coverImageUrl from cms_article WHERE delete_flag=0 and is_audit=1 and id =  ");
        hql.append("'" + id + "'");
        return this.querySqlObjects(hql.toString());*/
//        hql.append(" select t.id as id,t.title as title,t.createDate as createDate,t.url as url,t.type as type,t.state as state from ActivityAlerts t where t.deleteFlag = 0 order by t.updateDate desc ");
        hql.append(" select id,state,type,title,url,create_date from activity_alerts t where delete_flag = 0 order by update_date desc ");
        return this.querySqlObjects(hql.toString());
//        return this.queryByMapParams(hql.toString(), map);
    }

    @Override
    public List<Map<String, Object>> queryActivityAlertsTitleList(ActivityAlertsQueryDTO activityAlertsQueryDTO) {
        String id = activityAlertsQueryDTO.getId();
        StringBuilder hql = new StringBuilder();
        hql.append("select id,title,introduction as guidRead,url as image,date_format( create_date, '%Y-%m-%d' ) AS createDate ");
        if (activityAlertsQueryDTO != null) {
            if (!StringUtils.isEmpty(id)) {
                hql.append(",content");
            }
            hql.append(" from activity_alerts t where delete_flag = 0  ");
            String keyWord = activityAlertsQueryDTO.getKeyWord();
            if (StringUtils.isNotBlank(keyWord)) {
                hql.append(" and t.title like '%" + keyWord + "%'");
            }
            if (!StringUtils.isEmpty(id)) {
                hql.append(" and t.id = '" + id+"'");
            }
        }
        hql.append(" order by t.update_date desc limit 0,10 ");

        return this.querySqlObjects(hql.toString());
    }
}